﻿@{
    Layout = null;
    Context.Response.ContentType  = "text/xml";
}
<?xml version="1.0" encoding="utf-8"?>
<Module>

  <ModulePrefs
    description="Integrate to clarizen"
    scrolling="true"
    title="Clarizen cloud share"
    height="40"
    author="Daniel lehmann"
    author_email="daniel.lehmann@lehavi.com"
    author_location="Jerusalem">

    <OAuth>
      <Service name="docs1">
        <Access url="https://www.google.com/accounts/OAuthGetAccessToken" method="GET" />
        <Request url="https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feeds/" method="GET" />
        <Authorization url="https://www.google.com/accounts/OAuthAuthorizeToken?oauth_callback=http://oauth.gmodules.com/gadgets/oauthcallback" />
      </Service>
    </OAuth>
    <Require feature="dynamic-height"/>
    <Require feature="opensocial-0.8" />
    <Require feature="locked-domain"/>
    <Require feature="google.contentmatch">
      <Param name="extractors">
        google.com:SenderEmailExtractor,google.com:EmailTimeExtractor,google.com:RawSubjectExtractor,google.com:EmailBodyExtractor,google.com:MessageIDExtractor,google.com:RecipientCCEmailExtractor,google.com:RecipientToEmailExtractor
      </Param>
    </Require>

  </ModulePrefs>
  <Content type="html" view="card">
    <![CDATA[ 
  <!-- shindig oauth popup handling code -->
    <script src="https://gadget-doc-examples.googlecode.com/svn/trunk/opensocial-gadgets/popup.js"></script>
    <script src="@(Logic.Constants.CurrentUrl)/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script src="@(Logic.Constants.CurrentUrl)/Scripts/jquery-ui.js" type="text/javascript"></script>
    <script src="@(Logic.Constants.CurrentUrl)/Scripts/Site/SearchForm.js" type="text/javascript"></script>
    <script src="@(Logic.Constants.CurrentUrl)/Scripts/Site/TypeControlers.js" type="text/javascript"></script>

    <link href="@(Logic.Constants.CurrentUrl)/content/site.css" rel="stylesheet" type="text/css" />

  <style>
    #xmlBase  {font-size:11px; padding:20px;overflow:hidden}
  </style>
  <div id="xmlBase">
    <div id="main" style="display: none">
        <span class='link' onclick="fetchData('tasks/create')">click here</span> to get productive.
    </div>

    <div id="approval" style="display: none">
      <a href="#" id="personalize">login</a> (you only have to do this once)
    </div>

    <div id="waiting" style="display: none">
      Please click
      <a href="#" id="approvaldone">I've approved access</a>
      once you've approved access to your data.
    </div>
  </div>
  <script type="text/javascript">

       function showOneSection(toshow)
       {
           var sections = ['main', 'approval', 'waiting'];
           for (var i = 0; i < sections.length; ++i)
           {
               var s = sections[i];
               var el = document.getElementById(s);
               if (s === toshow)
               {
                   el.style.display = "block";
               } else
               {
                   el.style.display = "none";
               }
           }
       }

       function fetchData(endPoint, bringData)
       {
           var params = {};
           url = "@(Logic.Constants.CurrentUrl)/" + endPoint + "?i=" + Math.random(); ;
           params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
           params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
           params["OAUTH_SERVICE_NAME"] = "HMAC";
           params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
           gadgets.io.makeRequest(url, function (response)
           {
               if (response.data && response.data.RedirectUrl)
                   HandleLogin(response.data.RedirectUrl);
               else if (response.text)
               {
                   if (endPoint == 'login')
                   {
                       showOneSection('main');
                   }
                   else
                   {
                       showOneSection('main');
                       $('#main').html(response.text);
                       gadgets.window.adjustHeight();
                   }
               }
               else
                   ShowDebug(response);
           }, params);
       }



       function HandleLogin(url)
       {
           var popup = shindig.oauth.popup({
               destination: url,
               windowOptions: "width=350,height=250",
               onOpen: function () { showOneSection('waiting'); },
               onClose: function () { fetchData('tasks/create'); }
           });

           var personalize = document.getElementById('personalize');
           personalize.onclick = popup.createOpenerOnClick();
           var approvaldone = document.getElementById('approvaldone');
           approvaldone.onclick = popup.createApprovedOnClick();
           showOneSection('approval');
       }

       function ShowDebug(response)
       {
           // The response.oauthError and response.oauthErrorText values may help debug
           // problems with your gadget.
           var main = document.getElementById('main');
           var err = document.createTextNode('OAuth error: ' +
        response.oauthError + ': ' + response.oauthErrorText);
           main.appendChild(err);
           showOneSection('main');
       }
       // Call fetchData() when gadget loads.
       gadgets.util.registerOnLoadHandler(function () { fetchData('login', true) });
       window.setInterval(function () { fetchData('login', false); }, 300000);

       function stringFormat(str, arr)
       {
           if (!$.isArray(arr))
               arr = $.grep(arguments, function (item, index) { return index != 0; });

           return str.replace(/\{([0-9]+)\}/g, function (wholematch, firstmatch)
           {
               return arr[(+firstmatch)];
           });
       }

       function remoteRequest(options)
       {
           var params = {};
           params[gadgets.io.RequestParameters.CONTENT_TYPE] = options.contentType;
           params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
           params["OAUTH_SERVICE_NAME"] = "HMAC";
           params[gadgets.io.RequestParameters.METHOD] = options.type;

           var data = options.data;
           if (data)
           {
               if (typeof data != "string")
                   data = gadgets.io.encodeValues(data);
               params[gadgets.io.RequestParameters.POST_DATA] = data;
           }

           gadgets.io.makeRequest(options.url, function (res)
           {
               if (res && res.data && res.data.RedirectUrl)
                    HandleLogin(res.data.RedirectUrl);
               
               if (options.success)
               {
                   if (options.contentType == "JSON")
                   {
                       if (res.data)
                           options.success(res.data);
                       else
                           alert("Error")
                   }
                   else options.success(res.text);
               }
           }, params);
       }

       var encodeValues = gadgets.io.encodeValues;
       var adjustHeight = gadgets.window.adjustHeight;
       var global = {};
       global.matches = google.contentmatch.getContentMatches();
  </script>
  

  ]]>
  </Content>
</Module>



